import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/ui/home/page/home_page.dart';
import 'package:fzcgx_app/ui/order/page/order_page.dart';

class TabbarPage extends StatefulWidget {
  @override
  _TabbarPageState createState() => _TabbarPageState();
}

class _TabbarPageState extends State<TabbarPage> {
  int currentIndex;
  final pages = [HomePage(), OrderPage(), HomePage(), HomePage()];
  List titles = ["订单", "消息", "我的"];
  @override
  void initState() {
    super.initState();
    currentIndex = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: pages[currentIndex],
      bottomNavigationBar: BottomAppBar(
          color: Colors.white,
          child: Container(
            height: AdapterUtils.getHeight(60),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  tabbar(0),
                  tabbar(1),
                  tabbar(2),
                  tabbar(3),
                ]),
          )),
    );
  }

  // 自定义BottomAppBar
  Widget tabbar(int index) {
    //设置默认未选中的状态
    TextStyle style = TextStyle(
        fontSize: AdapterUtils.getSp(16),
        fontWeight: FontWeight.w500,
        color: Color(0xff99A2AD));
    Widget homeItem;
    Widget item;
    if (currentIndex == index) {
      //选中的话
      style = TextStyle(
          fontSize: AdapterUtils.getSp(16),
          fontWeight: FontWeight.w500,
          color: Color(0xff15A1FF));
    }
    if (index == 0) {
      if (currentIndex == index) {
        homeItem = GestureDetector(
          onTap: () {
            if (currentIndex != index) {
              if (mounted) {
                setState(() {
                  currentIndex = index;
                });
              }
            }
          },
          child: AnimatedContainer(
            duration: Duration(seconds: 2),
            child: Image.asset(
              AssetsUtils.getAssetsImagesPath('logo.png'),
              width: AdapterUtils.getWidth(38),
              height: AdapterUtils.getHeight(38),
            ),
          ),
        );
      } else {
        homeItem = GestureDetector(
          behavior: HitTestBehavior.opaque,
          child: AnimatedContainer(
              duration: Duration(seconds: 2),
              alignment: Alignment.center,
              margin: EdgeInsets.only(bottom: AdapterUtils.getHeight(16)),
              child: Text(
                '首页',
                style: style,
              )),
          onTap: () {
            if (currentIndex != index) {
              if (mounted) {
                setState(() {
                  currentIndex = index;
                });
              }
            }
          },
        );
      }
    } else {
      //构造返回的Widget
      item = AnimatedContainer(
        duration: Duration(seconds: 2),
        child: GestureDetector(
          behavior: HitTestBehavior.opaque,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Container(
                margin: currentIndex == index
                    ? EdgeInsets.all(0)
                    : currentIndex == 0
                        ? EdgeInsets.all(0)
                        : EdgeInsets.only(bottom: AdapterUtils.getHeight(6)),
                child: Text(
                  titles[index - 1],
                  style: style,
                ),
              ),
              currentIndex == 0
                  ? SizedBox()
                  : SizedBox(height: AdapterUtils.getHeight(10)),
              currentIndex == index
                  ? Container(
                      child: Image.asset(
                        AssetsUtils.getAssetsImagesPath('slider.png'),
                        width: AdapterUtils.getWidth(60),
                        height: AdapterUtils.getHeight(6),
                        fit: BoxFit.cover,
                      ),
                    )
                  : SizedBox(),
            ],
          ),
          onTap: () {
            if (currentIndex != index) {
              setState(() {
                currentIndex = index;
              });
            }
          },
        ),
      );
    }

    return index == 0 ? homeItem : item;
  }
}
